package com.sbl.bajw.common.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.sbl.bajw.util.JsonUtil;
import com.sbl.bajw.util.LogUtil;

public class CheckLoginFilter implements Filter {

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
	}
	
	static Logger log = null;
	
	static{
		log = LogUtil.getLogger("login");
	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain arg2) throws IOException, ServletException {
		String userid = SessionUnit.getUserId();
		if(userid == null || userid.equals("")){
			userid = RequestUnit.getString("user_id");
		}
		HttpServletResponse res = (HttpServletResponse)arg1;
		HttpServletRequest resq = (HttpServletRequest)arg0;
		String url= resq.getRequestURI();
		if(url.toLowerCase().indexOf("login") >= 0 
				|| url.toLowerCase().indexOf("register") >= 0 
				|| url.toLowerCase().indexOf("error") >= 0
				|| url.toLowerCase().indexOf("view/manager/forgetpwd.jsp") >=0
				|| url.toLowerCase().indexOf("view/manager/updatepwd.jsp") >=0 
				|| url.toLowerCase().indexOf("dept/getdept") >=0
				|| url.toLowerCase().indexOf("jcaptcha/check") >= 0 
				|| url.toLowerCase().indexOf("user/isuserthere") >=0
				|| url.toLowerCase().indexOf("user/register") >=0
				|| url.toLowerCase().indexOf("user/updatepwd") >=0
				|| url.toLowerCase().indexOf("user/identitycheck") >=0
				|| url.toLowerCase().indexOf("dictmodel/getposition") >=0
				|| url.toLowerCase().indexOf("dictmodel/getusertype") >=0
				|| url.toLowerCase().indexOf("qfqzmodel/getqfqzunit") >=0
				|| url.toLowerCase().indexOf("statistics") >=0
				|| url.toLowerCase().indexOf("spark") >=0
				|| url.toLowerCase().indexOf("jcaptcha") >=0
				|| url.toLowerCase().indexOf("apkdownload") >=0
				|| isResource(url) 
				|| userid != null){
			arg2.doFilter(arg0, arg1);
		}
		else if(userid == null || userid.equals("")){
			String sessionId = RequestUnit.getString("HttpSessionId");
			String isajax = RequestUnit.getString("is_ajax");
			log.info(url+" , "+sessionId);
			if(url.toLowerCase().indexOf("termpost") >= 0 || "true".equals(isajax)){
				PrintWriter writer = arg1.getWriter();
				HashMap<String, String> result = new HashMap<String, String>();
				result.put("msg", "请登录");
				result.put("success", "false");
				result.put("code", "4");
				String resultJson = JsonUtil.ConvertObjectToJson(result);
				writer.write(resultJson);
			}
			else{
				res.sendRedirect("http://"+resq.getServerName()+"/view/login.jsp");
			}
		}
	}
	
	public boolean isResource(String url){
		url = url.toLowerCase();
		int length = url.split("/").length;
		if(length < 1){
			return false;
		}
		String action = url.split("/")[length-1];
		int actionSize = action.split("\\.").length;
		if(actionSize>=2){
			String fileType = action.split("\\.")[actionSize-1];
			if(fileType.toLowerCase().equals("jsp") || fileType.toLowerCase().equals("do")){
				return false;
			}
			else{
				return true;
			}
		}
		else{
			return false;
		}
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}
